package com.ruoyi.web.controller.house;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.data.DataUtils;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.group.domain.HouseGroupT;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.unit.domain.HouseUnitT;
import com.ruoyi.unit.service.IHouseUnitTService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单元管理Controller
 *
 * @author lijie
 * @date 2023-04-19
 */
@RestController
@RequestMapping("/house/unit")
public class HouseUnitTController extends BaseController
{
    @Autowired
    private IHouseUnitTService houseUnitTService;

    /**
     * 查询单元管理列表
     */
     @GetMapping("/list")
    public TableDataInfo list(HouseUnitT houseUnitT)
    {
        startPage();
        HouseUnitT houseUnitT1 = new HouseUnitT();
        houseUnitT1.setAreaName(houseUnitT.getAreaName());
        houseUnitT1.setGroupName(houseUnitT.getGroupName());
        houseUnitT1.setUnitName(houseUnitT.getUnitName());
        List<HouseUnitT> list = houseUnitTService.selectHouseUnitTList(houseUnitT);
        for(int i = 0;i<list.size();i++) {
            //总户数&总面积
            HouseUnitT houseUnitVO = new HouseUnitT();
            houseUnitT.setAreaName(list.get(i).getAreaName());
            houseUnitT.setGroupName(list.get(i).getGroupName());
            houseUnitT.setUnitName(list.get(i).getUnitName());
            houseUnitVO = houseUnitTService.getDoorNumber(houseUnitT);
            String DoorNumber= String.valueOf(0d);
            DoorNumber=houseUnitVO.getDoorNumber();
            list.get(i).setDoorNumber(houseUnitVO.getDoorNumber());
            list.get(i).setSpaceNum(houseUnitVO.getSpaceNum());
            //应缴户数已缴户数未缴户数
            HouseUnitT unitVO = new HouseUnitT();
            houseUnitVO = houseUnitTService.getMust(houseUnitT);
            unitVO = houseUnitTService.getAlready(houseUnitT);
            list.get(i).setMustDoor(houseUnitVO.getMustDoor());
            list.get(i).setAlreadyDoor(unitVO.getAlreadyDoor());
            list.get(i).setNotDoor(houseUnitVO.getMustDoor()-unitVO.getAlreadyDoor());
            //已缴户数比率
            Double costRatio = 0d;
            if(!("0".equals(DoorNumber))){
                costRatio = DataUtils.doubleScale1(Double.valueOf(unitVO.getAlreadyDoor()) / Double.valueOf(DoorNumber)) * 100;
            }
            Integer costRatio1 = Integer.valueOf(costRatio.intValue());
            list.get(i).setCostRatio(String.valueOf(costRatio1) + "%");
            //已徼/未缴
            houseUnitVO = houseUnitTService.getGroupCost(houseUnitT);
            list.get(i).setBudgetMoney(houseUnitVO.getBudgetMoney());
            list.get(i).setCostMoney(houseUnitVO.getCostMoney());
            list.get(i).setCostMargin(DataUtils.costMargin(houseUnitVO.getBudgetMoney(),houseUnitVO.getCostMoney()));
        }
        if(getDataTableSumJudge(list)){
            HouseUnitT houseUnitT2 = new HouseUnitT();
            HouseUnitT houseUnitVO = new HouseUnitT();
            HouseUnitT unitVO = new HouseUnitT();
            houseUnitVO = houseUnitTService.getDoorNumber(houseUnitT1);
            houseUnitT2.setDoorNumber(houseUnitVO.getDoorNumber());
            houseUnitT2.setSpaceNum(houseUnitVO.getSpaceNum());
            houseUnitVO = houseUnitTService.getMust(houseUnitT1);
            unitVO = houseUnitTService.getAlready(houseUnitT1);
            houseUnitT2.setMustDoor(houseUnitVO.getMustDoor());
            houseUnitT2.setAlreadyDoor(unitVO.getAlreadyDoor());
            houseUnitT2.setNotDoor(houseUnitVO.getMustDoor()-unitVO.getAlreadyDoor());
            houseUnitVO = houseUnitTService.getGroupCost(houseUnitT1);
            houseUnitT2.setBudgetMoney(houseUnitVO.getBudgetMoney());
            houseUnitT2.setCostMoney(houseUnitVO.getCostMoney());
            houseUnitT2.setCostMargin(DataUtils.costMargin(houseUnitVO.getBudgetMoney(),houseUnitVO.getCostMoney()));
            if(houseUnitT2!=null){
                houseUnitT2.setAreaName("合计");
            }
            list.add(houseUnitT2);
        }
        return getDataTable(list);
    }

    /**
     * 导出单元管理列表
     */
    @Log(title = "单元管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseUnitT houseUnitT)
    {
        HouseUnitT houseUnitT1 = new HouseUnitT();
        houseUnitT1.setAreaName(houseUnitT.getAreaName());
        houseUnitT1.setGroupName(houseUnitT.getGroupName());
        houseUnitT1.setUnitName(houseUnitT.getUnitName());
        List<HouseUnitT> list = houseUnitTService.selectHouseUnitTList(houseUnitT);
        for(int i = 0;i<list.size();i++) {
            //总户数&总面积
            HouseUnitT houseUnitVO = new HouseUnitT();
            houseUnitT.setAreaName(list.get(i).getAreaName());
            houseUnitT.setGroupName(list.get(i).getGroupName());
            houseUnitT.setUnitName(list.get(i).getUnitName());
            houseUnitVO = houseUnitTService.getDoorNumber(houseUnitT);
            String DoorNumber= String.valueOf(0d);
            DoorNumber=houseUnitVO.getDoorNumber();
            list.get(i).setDoorNumber(houseUnitVO.getDoorNumber());
            list.get(i).setSpaceNum(houseUnitVO.getSpaceNum());
            //应缴户数已缴户数未缴户数
            HouseUnitT unitVO = new HouseUnitT();
            houseUnitVO = houseUnitTService.getMust(houseUnitT);
            unitVO = houseUnitTService.getAlready(houseUnitT);
            list.get(i).setMustDoor(houseUnitVO.getMustDoor());
            list.get(i).setAlreadyDoor(unitVO.getAlreadyDoor());
            list.get(i).setNotDoor(houseUnitVO.getMustDoor()-unitVO.getAlreadyDoor());
            //已缴户数比率
            Double costRatio = 0d;
            if(!("0".equals(DoorNumber))){
                costRatio = DataUtils.doubleScale1(Double.valueOf(unitVO.getAlreadyDoor()) / Double.valueOf(DoorNumber)) * 100;
            }
            Integer costRatio1 = Integer.valueOf(costRatio.intValue());
            list.get(i).setCostRatio(String.valueOf(costRatio1) + "%");
            //已徼/未缴
            houseUnitVO = houseUnitTService.getGroupCost(houseUnitT);
            list.get(i).setBudgetMoney(houseUnitVO.getBudgetMoney());
            list.get(i).setCostMoney(houseUnitVO.getCostMoney());
            list.get(i).setCostMargin(DataUtils.costMargin(houseUnitVO.getBudgetMoney(),houseUnitVO.getCostMoney()));
        }
        if(getDataTableSumJudge(list)){
            HouseUnitT houseUnitT2 = new HouseUnitT();
            HouseUnitT houseUnitVO = new HouseUnitT();
            HouseUnitT unitVO = new HouseUnitT();
            houseUnitVO = houseUnitTService.getDoorNumber(houseUnitT1);
            houseUnitT2.setDoorNumber(houseUnitVO.getDoorNumber());
            houseUnitT2.setSpaceNum(houseUnitVO.getSpaceNum());
            houseUnitVO = houseUnitTService.getMust(houseUnitT1);
            unitVO = houseUnitTService.getAlready(houseUnitT1);
            houseUnitT2.setMustDoor(houseUnitVO.getMustDoor());
            houseUnitT2.setAlreadyDoor(unitVO.getAlreadyDoor());
            houseUnitT2.setNotDoor(houseUnitVO.getMustDoor()-unitVO.getAlreadyDoor());
            houseUnitVO = houseUnitTService.getGroupCost(houseUnitT1);
            houseUnitT2.setBudgetMoney(houseUnitVO.getBudgetMoney());
            houseUnitT2.setCostMoney(houseUnitVO.getCostMoney());
            houseUnitT2.setCostMargin(DataUtils.costMargin(houseUnitVO.getBudgetMoney(),houseUnitVO.getCostMoney()));
            if(houseUnitT2!=null){
                houseUnitT2.setAreaName("合计");
            }
            list.add(houseUnitT2);
        }
        ExcelUtil<HouseUnitT> util = new ExcelUtil<HouseUnitT>(HouseUnitT.class);
        util.exportExcel(response, list, "单元管理数据");
    }

    /**
     * 获取单元管理详细信息
     */
   @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") Long unitId)
    {
        return AjaxResult.success(houseUnitTService.selectHouseUnitTByUnitId(unitId));
    }

    /**
     * 新增单元管理
     */
     @Log(title = "单元管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseUnitT houseUnitT)
    {
        houseUnitT.setCreater(getUsername());
        return toAjax(houseUnitTService.insertHouseUnitT(houseUnitT));
    }

    /**
     * 修改单元管理
     */
     @Log(title = "单元管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseUnitT houseUnitT)
    {
        houseUnitT.setCreater(getUsername());
        return toAjax(houseUnitTService.updateHouseUnitT(houseUnitT));
    }

    /**
     * 删除单元管理
     */
    @Log(title = "单元管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds)
    {
        HouseUnitT houseUnitVO=new HouseUnitT();
        HouseUnitT unitVO=new HouseUnitT();
        for(Long unitId : unitIds){
            houseUnitVO = houseUnitTService.selectHouseUnitTByUnitId(unitId);
            unitVO.setUnitName(houseUnitVO.getUnitName());
            unitVO.setGroupName(houseUnitVO.getGroupName());
            unitVO.setAreaName(houseUnitVO.getAreaName());
            List<HouseUnitT> list= new ArrayList<HouseUnitT>();
            list =houseUnitTService.selectHouseDoorTByDoorName(unitVO);
            for (int i=0;i< list.size();i++) {
                if ((list.get(i).getDoorName() != null)) {
                    return AjaxResult.error("存在户,不允许删除");
                }
            }
        }
        return toAjax(houseUnitTService.deleteHouseUnitTByUnitIds(unitIds));
    }

    /**
     * 获取单元
     */
    @GetMapping(value = "/getUnitInfo/{areaName}/{groupName}" )
    public TableDataInfo getUnitInfo(@PathVariable("areaName") String areaName,@PathVariable("groupName") String groupName)
    {
        HouseUnitT houseUnitT= new HouseUnitT();
        houseUnitT.setAreaName(areaName);
        houseUnitT.setGroupName(groupName);
        List<HouseUnitT> list = houseUnitTService.selectHouseUnitTList(houseUnitT);
        return getDataTable(list);
    }

    @Log(title = "单元迁移管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/moveUnit")
    public AjaxResult moveUnit(@RequestBody HouseUnitT houseUnitT)
    {
//        HouseUnitT vo=houseUnitTService.selectUnit(houseUnitT);
//        if (vo!=null){
//            return AjaxResult.error("该小区楼宇下已有此单元,请进行户管理下户迁移功能  !");
//    }
        return toAjax(houseUnitTService.updateHouseUnitT(houseUnitT));
    }

}
